package cn.edu.pzhu.javaweb.controller;

import cn.edu.pzhu.javaweb.pojo.Userinfo;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userinfo")
public class UserinfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public UserinfoServlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = (String) request.getSession().getAttribute("username");//从session中获取username
        Userinfo userinfo = (Userinfo) request.getServletContext().getAttribute(username+"_userinfo");//从注册时存储的userinfo中获取
        request.getSession().setAttribute("userinfo", userinfo);//将userinfo存储到session中

        List<String> types = JSON.parseArray(userinfo.getType(), String.class);
        request.getSession().setAttribute("types", types);

        response.sendRedirect("showuserinfo.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
